package ${baseInfo.basePackage}.${baseInfo.moduleName}.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.poi.excel.Excel;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import ${baseInfo.basePackage}.${baseInfo.moduleName}.dao.${baseInfo.classForClassName}Dao;
import ${baseInfo.basePackage}.${baseInfo.moduleName}.model.${baseInfo.classForClassName};
<#if selectFlow?? && selectFlow != "">
import com.ccp.dev.workflow.model.ProcessRun;
import com.ccp.dev.workflow.model.bpm.ProcessCmd;
import com.ccp.dev.workflow.service.ProcessRunService;
</#if>
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * ${baseInfo.tableComment} 信息操作服务类
 *
 * @author ${baseInfo.author}
 * @date ${baseInfo.datetime}
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ${baseInfo.classForClassName}Service extends BaseService<${baseInfo.classForClassName}> {

    @Resource
    private ${baseInfo.classForClassName}Dao dao;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
    List<${baseInfo.classForClassName}> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (${baseInfo.classForClassName} data : dataList) {
        excel.cell(startRow, 0).value(data.get${baseInfo.primaryKey.attrNameForMethod}());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

<#if selectFlow?? && selectFlow != "">
    @Resource
    private ProcessRunService processRunService;

    /**
    * 流程处理器方法 用于处理业务数据
    * @param cmd
    */
    public void processHandler(ProcessCmd cmd){
        Map data=cmd.getFormDataMap();
        if(BeanUtils.isNotEmpty(data)){
            <#list baseInfo.columns as column>
            <#if column.dataType?? && column.dataType == "blob">
            String ${column.attrNameForProperty} = data.get("${baseInfo.attrNameForProperty}").toString();
            ${column.attrType} ${column.attrNameForProperty}ToByte = ${column.attrNameForProperty}.getBytes();
            data.put("${column.attrNameForProperty}",${column.attrNameForProperty}ToByte);
            </#if>
            </#list>
            JSONObject obj = JSONObject.fromObject(data);
            ${baseInfo.classForClassName} ${baseInfo.classForPrefix} = (${baseInfo.classForClassName})JSONObject.toBean(obj, ${baseInfo.classForClassName}.class);
            if(StringUtil.isEmpty(cmd.getBusinessKey())){
                String genId=UUIDUtils.getUUIDFor32();
                ${baseInfo.classForPrefix}.set${baseInfo.primaryKey.attrNameForMethod}(genId);
                <#list baseInfo.columns as column>
                <#if column.columnName == "tenantid">
                ${baseInfo.classForPrefix}.setTenantId(ContextUtil.getCurrentUserTenantId());
                </#if>
                <#if column.columnName == "createby">
                ${baseInfo.classForPrefix}.setCreateBy(ContextUtil.getCurrentUserId());
                </#if>
                <#if column.columnName == "createtime">
                ${baseInfo.classForPrefix}.setCreateTime(new Date());
                </#if>
                </#list>
                this.add(${baseInfo.classForPrefix});
            }else{
                ${baseInfo.classForPrefix}.set${baseInfo.primaryKey.attrNameForMethod}(cmd.getBusinessKey());
                <#list baseInfo.columns as column>
                <#if column.columnName == "updateby">
                ${baseInfo.classForPrefix}.setUpdateBy(ContextUtil.getCurrentUserId());
                </#if>
                <#if column.columnName == "updatetime">
                ${baseInfo.classForPrefix}.setUpdateTime(new Date());
                </#if>
                </#list>
                this.update(${baseInfo.classForPrefix});
            }
            cmd.setBusinessKey(${baseInfo.classForPrefix}.get${baseInfo.primaryKey.attrNameForMethod}());
        }
    }

    /**
     * 重写getAll方法绑定流程runId
     */
     @Override
     public List<${baseInfo.classForClassName}> queryList(QueryFilter queryFilter){
        List<${baseInfo.classForClassName}> ${baseInfo.classForPrefix}List = super.queryList(queryFilter);
        List<${baseInfo.classForClassName}> ${baseInfo.classForPrefix}s = new ArrayList<${baseInfo.classForClassName}>();
        for(${baseInfo.classForClassName} ${baseInfo.classForPrefix} : ${baseInfo.classForPrefix}List){
            ProcessRun processRun = processRunService.getByBusinessKey(${baseInfo.classForPrefix}.get${baseInfo.primaryKey.attrNameForMethod}());
            if(BeanUtils.isNotEmpty(processRun)){
                ${baseInfo.classForPrefix}.setRunId(processRun.getRunId());
            }
            ${baseInfo.classForPrefix}s.add(${baseInfo.classForPrefix});
        }
        return ${baseInfo.classForPrefix}s;
    }
</#if>
}
